"use strict";
cc._RF.push(module, 'bebebwHpyxB9q6Me+7IbLb/', 'CollisionGrid');
// scripts/CollisionGrid.ts

Object.defineProperty(exports, "__esModule", { value: true });
var CollisionGrid = /** @class */ (function () {
    function CollisionGrid(width, height, gridSize) {
        this._width = width;
        this._height = height;
        this._gridSize = gridSize;
        this._numCols = Math.ceil(width / gridSize); //计算总列数            
        this._numRows = Math.ceil(height / gridSize); //计算总行数
        this._numCells = this._numCols * this._numRows; //单元格总数
        var size = cc.view.getVisibleSize();
        this.offsetX = size.width * 0.5;
        this.offsetY = size.height * 0.5;
    }
    //将需要检测的对象(泛型)数组objects分配到网络
    CollisionGrid.prototype.assign = function (objects) {
        var numObjects = objects.length;
        this._grid = [];
        this._checks = [];
        for (var i = 0; i < numObjects; i++) {
            var obj = objects[i];
            var tx = obj.x + this.offsetX;
            var ty = obj.y + this.offsetY;
            //注意：这里用“Grid.[索引]”（定位）的方式，替换了原来的“Grid.[列][行]”（单元格的定位）方式--回想一下bitmap位图中的像素索引就更容易理解了
            var index = Math.floor(ty / this._gridSize) * this._numCols + Math.floor(tx / this._gridSize);
            //“单元格”--延时实例化"
            if (this._grid[index] == null) {
                this._grid[index] = [];
            }
            //将对象推入"单元格"
            this._grid[index].push(obj);
        }
        //检测需要碰撞的对象，并保存到_checks数组
        this.checkGrid();
    };
    //"单元格"检测
    CollisionGrid.prototype.checkGrid = function () {
        for (var i = 0; i < this._numCols; i++) {
            for (var j = 0; j < this._numRows; j++) {
                this.checkOneCell(i, j);
                this.checkTwoCells(i, j, i + 1, j);
                this.checkTwoCells(i, j, i - 1, j + 1);
                this.checkTwoCells(i, j, i, j + 1);
                this.checkTwoCells(i, j, i + 1, j + 1);
            }
        }
    };
    //(自身)单个单元格的检测
    CollisionGrid.prototype.checkOneCell = function (x, y) {
        var cell = this._grid[y * this._numCols + x];
        if (cell == null) {
            return;
        }
        var cellLength = cell.length;
        for (var i = 0; i < cellLength - 1; i++) {
            var objA = cell[i];
            for (var j = i + 1; j < cellLength; j++) {
                var objB = cell[j];
                this._checks.push(objA, objB);
            }
        }
    };
    //单元格(x1,y1)与单元格(x2,y2)的检测
    CollisionGrid.prototype.checkTwoCells = function (x1, y1, x2, y2) {
        if (x2 >= this._numCols || x2 < 0 || y2 >= this._numRows) {
            return;
        }
        var cellA = this._grid[y1 * this._numCols + x1];
        var cellB = this._grid[y2 * this._numCols + x2];
        if (cellA == null || cellB == null) {
            return;
        }
        var cellALength = cellA.length;
        var cellBLength = cellB.length;
        for (var i = 0; i < cellALength; i++) {
            var objA = cellA[i];
            for (var j = 0; j < cellBLength; j++) {
                var objB = cellB[j];
                this._checks.push(objA, objB);
            }
        }
    };
    Object.defineProperty(CollisionGrid.prototype, "checks", {
        get: function () {
            return this._checks;
        },
        enumerable: false,
        configurable: true
    });
    return CollisionGrid;
}());
exports.default = CollisionGrid;

cc._RF.pop();